//
//  MainItem.m
//  wmh.ios.v5
//
//  Created by AndLi on 14/10/22.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "MainItem.h"

@implementation MainItem

@synthesize image;
@synthesize itemid;
@synthesize titleLabel;
@synthesize itemImageView;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.delegate = self;
        //		self.minimumZoomScale = 0.5;
        //		self.maximumZoomScale = 2.5;
        self.showsVerticalScrollIndicator = NO;
        self.showsHorizontalScrollIndicator = NO;
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        imageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:imageView];
        if (IS_IPHONE_5) {
            titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0+5, imageView.frame.size.height-30, 90, 30)];
        }else{
            titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, imageView.frame.size.height-30, 90, 30)];
        }
        
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:titleLabel];
        if (IS_IPHONE_5) {
            itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25+5, 20, 40, 40)];
        }else{
            itemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 20, 40, 40)];
        }
        
        itemImageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:itemImageView];
    }
    return self;
}



- (void)setImage:(UIImage *)img {
    imageView.image = img;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    CGFloat zs = scrollView.zoomScale;
    zs = MAX(zs, 1.0);
    zs = MIN(zs, 2.0);
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    scrollView.zoomScale = zs;
    [UIView commitAnimations];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    
    if ([touch tapCount] == 2) {
        CGFloat zs = self.zoomScale;
        zs = (zs == 1.0) ? 2.0 : 1.0;
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        self.zoomScale = zs;
        [UIView commitAnimations];
    }
}

#pragma mark 触发点击事件
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.dele clickMainItem:itemid];
}



@end